home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / packages / resume.el < prev    next >
Encoding:
Text File  |  1995-01-31  |  5.3 KB  |  130 lines

  1. ;;; resume.el --- process command line args from within a suspended Emacs job
  2. ;; Keywords: processes
  3.  
  4. ;; Copyright (C) 1988 Free Software Foundation, Inc.
  5.  
  6. ;; This file is not yet part of GNU Emacs, but soon will be.
  7.  
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  10. ;; accepts responsibility to anyone for the consequences of using it
  11. ;; or for whether it serves any particular purpose or works at all,
  12. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  13. ;; License for full details.
  14.  
  15. ;; Everyone is granted permission to copy, modify and redistribute
  16. ;; GNU Emacs, but only under the conditions described in the
  17. ;; GNU Emacs General Public License.   A copy of this license is
  18. ;; supposed to have been given to you along with GNU Emacs so you
  19. ;; can know your rights and responsibilities.  It should be in a
  20. ;; file named COPYING.  Among other things, the copyright notice
  21. ;; and this notice must be preserved on all copies.
  22. ;;
  23. ;; by Joe Wells
  24. ;; jbw@bucsf.bu.edu
  25. ;; joew@uswat.uswest.com (maybe, ... the mailer there sucks)
  26.  
  27. ;; Stephan Gildea suggested bug fix (gildea@bbn.com).
  28. ;; Ideas from Michael DeCorte and other people.
  29.  
  30. ;; For csh users, insert the following alias in your .cshrc file
  31. ;; (after removing the leading double semicolons):
  32. ;;
  33. ;;# The following line could be just EMACS=emacs, but this depends on
  34. ;;# your site.
  35. ;;set EMACS=emacs
  36. ;;set EMACS_PATTERN="^\[[0-9]\]  . Stopped ............ $EMACS"
  37. ;;alias emacs \
  38. ;;' \\
  39. ;;   jobs >! /tmp/jobs$$ \\
  40. ;;   && grep "$EMACS_PATTERN" /tmp/jobs$$ >& /dev/null \\
  41. ;;   && echo `pwd` \!* >! ~/.emacs_args && eval "%$EMACS" \\
  42. ;;|| test -S ~/.emacs_server && emacsclient \!* \\
  43. ;;|| test "$?DISPLAY" = 1 && eval "\$EMACS -i \!* &" \\
  44. ;;|| test "$?WINDOW_PARENT" = 1 && eval "emacstool -f emacstool-init \!* &" \\
  45. ;;|| eval "\$EMACS -nw \!*"'
  46. ;;
  47. ;; The alias works as follows:
  48. ;; 1. If there is a suspended emacs jobs that is a child of the
  49. ;; current shell, place its arguments in the ~/.emacs_args file and
  50. ;; resume it.
  51. ;; 2. Else if the ~/.emacs_server socket has been created, presume an
  52. ;; emacs server is running and attempt to connect to it.  If no emacs
  53. ;; server is listening on the socket, this will fail.
  54. ;; 3. Else if the DISPLAY environment variable is set, presume we are
  55. ;; running under X Windows and start a new X Gnu Emacs process in the
  56. ;; background.
  57. ;; 4. Else if the WINDOW_PARENT environment variable is set, presume we
  58. ;; are running under Sunview and Suntools and start an emacstool
  59. ;; process in the background.
  60. ;; 5. Else start a regular emacs process.
  61. ;;
  62. ;; Notes:
  63. ;; "test -S" checks if a unix domain socket by that name exists.
  64. ;; The output of the "jobs" command is not piped directly into "grep"
  65. ;; because that would run the "jobs" command in a subshell.
  66. ;; Before resuming a suspended emacs, the current directory and all
  67. ;; command line arguments are placed in a file.
  68. ;; The command to run emacs is always preceded by a \ to prevent
  69. ;; possible alias loops.
  70. ;; The "-nw" switch in the last line is is undocumented, and it means
  71. ;; no windowing system.
  72.  
  73. (setq suspend-resume-hook 'resume-process-args)
  74. (setq suspend-hook 'resume-preparation)
  75.  
  76. (defvar emacs-args-file "~/.emacs_args"
  77.   "*This file is where arguments are placed for a suspended emacs job.")
  78.  
  79. (defun resume-preparation ()
  80.   (condition-case ()
  81.       (delete-file emacs-args-file)
  82.     (error nil)))
  83.  
  84. (defun resume-process-args ()
  85.   "This should be called from inside of suspend-resume-hook.
  86. Grabs the contents of the file whose name is stored in
  87. emacs-args-file, and processes these arguments like command line options."
  88.   (let ((resume-start-buffer (current-buffer))
  89.     (resume-args-buffer (get-buffer-create " *Command Line Args*"))
  90.     resume-args)
  91.     (unwind-protect
  92.     (progn
  93.       (set-buffer resume-args-buffer)
  94.       (erase-buffer)
  95.       ;; Get the contents of emacs-args-file, then delete the file.
  96.       (condition-case ()
  97.           (progn
  98.         (insert-file-contents emacs-args-file)
  99.         (delete-file emacs-args-file))
  100.         ;; The file doesn't exist or we can't delete it, ergo no arguments.
  101.         ;; (If we can't delete it now, we probably couldn't delete it
  102.         ;; before suspending, and that implies it may be vestigial.)
  103.         (file-error (erase-buffer)))
  104.       ;; Get the arguments from the buffer.
  105.       (goto-char (point-min))
  106.       (while (progn (skip-chars-forward " \t\n") (not (eobp)))
  107.         (setq resume-args
  108.           (cons (buffer-substring (point)
  109.                       (progn
  110.                         (skip-chars-forward "^ \t\n")
  111.                         (point)))
  112.                  resume-args)))
  113.       (cond (resume-args
  114.          ;; Arguments are now in reverse order.
  115.          (setq resume-args (nreverse resume-args))
  116.          ;; The "first argument" is really a default directory to use
  117.          ;; while processing the rest of the arguments.
  118.          (setq default-directory (concat (car resume-args) "/"))
  119.          ;; Actually process the arguments.
  120.          (command-line-1  (cdr resume-args)))))
  121.       ;; If the command line args don't result in a find-file, the
  122.       ;; buffer will be left in resume-args-buffer.  So we change back to the
  123.       ;; original buffer.  The reason I don't just use
  124.       ;; (let ((default-directory foo))
  125.       ;;    (command-line-1 args))
  126.       ;; in the context of the original buffer is because let does not
  127.       ;; work properly with buffer-local variables.
  128.       (if (eq (current-buffer) resume-args-buffer)
  129.       (set-buffer resume-start-buffer)))))
  130.